package com.ems.service.impl.supers.questions;

import com.ems.dao.supers.questions.ChooseDao;
import com.ems.dao.supers.questions.answer.ChooseAnswerDao;
import com.ems.pojo.data.RequestData;
import com.ems.pojo.questions.Choose;
import com.ems.pojo.questions.Subject;
import com.ems.pojo.questions.answer.ChooseAnswer;
import com.ems.service.supers.questions.ChooseService;
import com.ems.service.supers.questions.SubjectService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: 伟伟
 * @Date: 2019/10/19
 * @Description: 描述
 * @Version: 1.0
 */
@Service("chooseService")
public class ChooseServiceImpl implements ChooseService {

    @Autowired
    private ChooseDao chooseDao;

    @Autowired
    private ChooseAnswerDao chooseAnswerDao;

    @Autowired
    @Qualifier("subjectService")
    private SubjectService subjectService;

    /**
     * 添加选择题
     * @param choose
     */
    @Override
    public void addChoose(Choose choose) {
       chooseDao.addChoose(choose);
    }

    /**
     * 删除指定的选择题
     * @param chooseList
     */
    @Override
    public void deleteChoose(List<Choose> chooseList) {
        for(int i = 0;i < chooseList.size();i ++){
            //删除对应的答案
            chooseAnswerDao.deleteChooseAnswer(new ChooseAnswer(chooseList.get(i).getCh_id()));
            //删除选择题
            chooseDao.deleteChoose(chooseList.get(i));

        }
    }

    /**
     * 修改指定的选择题
     * @param choose
     */
    @Override
    public void updateChoose(Choose choose) {
        //删除对应的答案
        chooseAnswerDao.deleteChooseAnswer(new ChooseAnswer(choose.getCh_id()));
        //修改对应的选择题
        chooseDao.updateChoose(choose);

        ChooseAnswer chooseAnswer = null;

        //添加对应的答案
        for(int i =0;i < choose.getChooseAnswerList().size(); i ++){
            chooseAnswer = choose.getChooseAnswerList().get(i);
            chooseAnswer.setCh_id(choose.getCh_id());
            chooseAnswerDao.addChooseAnswer(chooseAnswer);
        }
    }

    /**
     * 根据条件查询选择题
     * @param requestData
     * @return
     */
    @Override
    public List<Choose> findChoose(RequestData requestData) {
        List<Choose> list = chooseDao.findChoose(requestData.getMap());

        if(list != null && list.size() > 0){
            Choose choose = null;
           for(int i = 0;i < list.size();i ++){
               choose = list.get(i);

               Subject subject = subjectService.getSubNameBySbId(choose.getSb_id());

               choose.setSb_name(subject.getSb_name());

           }

        }

        return list;
    }

    @Override
    public void addNewChoose(Choose choose) {
        int n = chooseDao.addNewChoose(choose);
//        System.out.println(n);
//        System.out.println(choose);
        ChooseAnswer chooseAnswer = null;
        for(int i = 0;i < choose.getChooseAnswerList().size();i ++){
            chooseAnswer = choose.getChooseAnswerList().get(i);
            chooseAnswer.setCh_id(choose.getCh_id());
            chooseAnswerDao.addChooseAnswer(chooseAnswer);
        }

    }

    @Override
    public Choose getChooseById(Choose choose) {

        return null;
    }

    //根据类型获取选择题的数量
    @Override
    public int chooseSumByType(int sb_id,int ch_type) {
        return chooseDao.chooseSumByType(sb_id,ch_type);
    }

    //根据课目和类型查询选择题
    @Override
    public List<Choose> getChooseBySbAndId(int sb_id,int ch_type,int num) {
        return chooseDao.getChooseBySbAndId(sb_id,ch_type,num);
    }

    //根据id集合查询选择题
    @Override
    public List<Choose> getChooseByIdsList(List<Integer> idsList) {
        return chooseDao.getChooseByIdsList(idsList);
    }

    //获取单选题答案
    @Override
    public ChooseAnswer getAnswer(int id) {
        ChooseAnswer chooseAnswer = chooseAnswerDao.getAnswer(id);
        return chooseAnswer;
    }

    //获取多选题答案
    @Override
    public List<ChooseAnswer> getAnswerList(int id) {
        List<ChooseAnswer> list = chooseAnswerDao.getAnswerList(id);
        return list;
    }
}
